<template>
  <div class="reg-container">
    <!-- 头部的 logo 区域 -->
    <div class="header">
      <img src="../../assets/images/logo.png" alt="" />
    </div>

    <!-- 登录和注册区域 -->
    <div class="box">
      <div class="box-header"></div>
      <!-- 注册的表单 -->
      <el-form :model="regFrom" :rules="regRules" ref="regRef">
        <el-form-item prop="username">
          <el-input
          v-model="regFrom.username"
            placeholder="请输入用户名"
            maxlength="10"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
          v-model="regFrom.password"
            placeholder="请输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
          v-model="regFrom.repassword"
            placeholder="请再次输入密码"
            maxlength="15"
            type="password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-button type="primary" class="btn-login" @click="reg('regRef')">注册</el-button>
        <el-link :underline="false" type="info" class="link-reg" @click="$router.push('/login')">去登录</el-link>
      </el-form>
    </div>
  </div>
</template>

<script>
import { regPost } from '@/api/user'
export default {
  name: 'Reg',
  data () {
    // 定义:自定义校验规则
    const validatePass = (rule, value, callback) => {
      if (value !== this.regFrom.password) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }

    return {
      regFrom: {
        username: '',
        password: '',
        repassword: ''
      },
      regRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '用户名必须是1-10位的字母数字',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符串',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符串',
            trigger: 'blur'
          },
          // validator使用自定义校验规则:validatePass
          { validator: validatePass, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (!valid) return console.log('fail')
        console.log('success')
        const { data: { code, message } } = await regPost(this.regFrom)
        if (code === 0) {
          this.$message.success(message)
          this.$router.push('/login')
        } else {
          this.$message.error(message)
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background-color: blue;
  height: 100%;
  background: url('../../assets/images/login_bg.jpg') no-repeat center;
  background-size: cover;

  .header {
    width: 1200px;
    margin: 0 auto;
    user-select: none;
  }

  .box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    box-shadow: 0 1px 15px rgba(0, 0, 0, 0.2);
    padding: 0 30px;
    box-sizing: border-box;

    .box-header {
      height: 60px;
      background: url('../../assets/images/login_title.png') no-repeat center;
    }

    .btn-login {
      width: 100%;
    }

    .link-reg {
      float: right;
      font-size: 12px;
    }
  }
}
</style>
